<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ListMenu;

use ListMenu\Model\ListMenu;
use ListMenu\Model\ListMenuTable;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Session\Container;
use ListMenu\View\Helper\Users as UsersHelper;
use ListMenu\Model\User as UserModel;


class Module
{
    public function onBootstrap(MvcEvent $e)
    {
    	date_default_timezone_set('Asia/Ho_Chi_Minh');
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        
    }

    public function onApplicationPreDispacth($e)
    {
    	$session_name = session_name();
    
    	if (isset($_POST[$session_name])) {
    		session_destroy();
    		session_id($_POST[$session_name]);
    		session_start();
    	}
    }
    
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
    
    
    // getAutoloaderConfig() and getConfig() methods here
    
    // Add this method:
    public function getServiceConfig()
    {
    	return array(

    			'factories' => array(    					
    					'ListMenu\Model\ListMenuTable' =>  function($sm) {
    						$tableGateway = $sm->get('ListMenuTableGateway');
    						$table = new ListMenuTable($tableGateway);
    						return $table;
    					},
    					'ListMenuTableGateway' => function ($sm) {
    						$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
    						$resultSetPrototype = new ResultSet();
    						$resultSetPrototype->setArrayObjectPrototype(new ListMenu());
    						return new TableGateway('menu', $dbAdapter, null, $resultSetPrototype);
    					},
    					
    					'ListMenu\Model\Data' => function ($sm) {
    						$dbAdapter = $sm->get( 'Zend\Db\Adapter\Adapter' );
    						$table = new UserModel( $dbAdapter );
    						return $table;
    					},
    			),		
    	);
    }
    
    public function getViewHelperConfig() {
    	return array (
    			'factories' => array (
    					'user_helper' => function($sm) {
    						$session = new Container('User');
    						$email = $session->offsetGet('email');
    						$helper_user = new UsersHelper();
    						$helper_user->setTableUser ( $sm->getServiceLocator(), 'ListMenu\Model\Data' ,$email);
    						return $helper_user;
    					}
    			)
    	);
    }

}
